// pages/iputPostNumber/inputPostNumber.js
import tips from '../../lib/tips';
var util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    postNumber: "",
    transferId: "",
    id:"",
    tipsData: {
      title: '',
      isHidden: true
    },
    openId: app.globalData.openId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.transferId = options.transferId;
    this.data.id = options.id;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //监听内容输入
  listenerNumberInput(e) {
    this.data.postNumber = e.detail.value;
  },
  showToast(title, duartion) {
    const that = this;
    const tipsData = {
      title: title || '',
      duartion: duartion || 2000,
      isHidden: false
    };
    tips.toast(that.data.tipsData);
    that.setData({
      tipsData
    });
    setTimeout(() => {
      tipsData.isHidden = true;
      that.setData({
        tipsData
      });
    }, tipsData.duartion);
  },
  formSubmit: util.throttle(function (e) {
    const that = this;
    if (!this.data.postNumber) {
      that.showToast('请输入您要发货的快递单号');
      return;
    }
    wx.request({
      url: app.globalData.apiDomain + '/api/order/sendGoods',
      method: "POST",
      data: {
        postNumber: this.data.postNumber,
        transferId: this.data.transferId,
        id: this.data.id
      },
      success: function (res) {
        wx.redirectTo({
          url: '../myTransfer/myTransfer',
          complete: function () {
            wx.showToast({
              title: '发货成功',
              image: '../../images/correct_toast.png',
              duration: 2000,
            });
          }
        })
      }
    })
  })
})